package com.cyt.work.divideclass.service.impl;

import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.*;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.divideclass.dao.DivideclassClassDao;
import com.cyt.work.divideclass.dao.DivideclassDao;
import com.cyt.work.divideclass.dao.DivideclassRuleDao;
import com.cyt.work.divideclass.model.po.Divideclass;
import com.cyt.work.divideclass.model.po.DivideclassClass;
import com.cyt.work.divideclass.model.po.DivideclassRule;
import com.cyt.work.divideclass.model.vo.DivideclassClassVo;
import com.cyt.work.divideclass.model.vo.DivideclassRuleVo;
import com.cyt.work.divideclass.service.DivideclassClassService;
import com.cyt.work.divideclass.service.DivideclassRuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 智能分班分班设置-智能分班分班设置Service层实现类
 *
 * @author caideyong
 * @date 2019-12-20 14:24:06
 */
@Service("divideclassRuleService")
public class DivideclassRuleServiceImpl extends BaseServiceImpl<DivideclassRule> implements DivideclassRuleService {

    @Resource
    private DivideclassRuleDao divideclassRuleDao;
    @Autowired
    private DivideclassClassDao divideclassClassDao;
    @Autowired
    private DivideclassClassService divideclassClassService;
    @Autowired
    private DivideclassDao divideclassDao;
    @Autowired
    public void setBaseDao(DivideclassRuleDao divideclassRuleDao) {
        this.baseDao = divideclassRuleDao;
    }

    /**
     * 查询对象集合
     *
     * @param divideclassRuleVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-12-20 14:24:06
     */
    @Override
    public List<DivideclassRule> listExample(DivideclassRuleVo divideclassRuleVo) {
        return divideclassRuleDao.getDivideclassRuleList(divideclassRuleVo);
    }

    /**
     * 分页查询
     *
     * @param divideclassRuleVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:24:06
     */
    @Override
    public ReturnResult page(DivideclassRuleVo divideclassRuleVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<DivideclassRule> list = listExample(divideclassRuleVo);
        PageInfo<DivideclassRule> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param divideclassRuleVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:24:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(DivideclassRuleVo divideclassRuleVo, SysUser sysUser) {
        if (StringUtils.isBlank(divideclassRuleVo.getId())) {
            DivideclassRule divideclassRule = new DivideclassRule(sysUser);
            divideclassRule.setId(BaseUtil.uId());
            divideclassRule.setClassNum(divideclassRuleVo.getClassNum());
            divideclassRule.setIsScore(divideclassRuleVo.getIsScore());
            divideclassRule.setTotalError(divideclassRuleVo.getTotalError());
            divideclassRule.setIsRank(divideclassRuleVo.getIsRank());
            divideclassRule.setIsSex(divideclassRuleVo.getIsSex());
            divideclassRule.setDivideId(divideclassRuleVo.getDivideId());
            divideclassRule.setCreator(divideclassRuleVo.getCreator());
            divideclassRuleDao.insert(divideclassRule);
            if(StringUtils.isNotEmpty(divideclassRuleVo.getClassArr())){
                divideclassRuleVo.setId(divideclassRule.getId());
                saveClassArr(divideclassRuleVo,sysUser);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            DivideclassRule divideclassRule = divideclassRuleDao.selectByPrimaryKey(divideclassRuleVo.getId());
            divideclassRule.setClassNum(divideclassRuleVo.getClassNum());
            divideclassRule.setIsScore(divideclassRuleVo.getIsScore());
            divideclassRule.setTotalError(divideclassRuleVo.getTotalError());
            divideclassRule.setIsRank(divideclassRuleVo.getIsRank());
            divideclassRule.setIsSex(divideclassRuleVo.getIsSex());
            divideclassRule.setDivideId(divideclassRuleVo.getDivideId());
            divideclassRule.setCreator(divideclassRuleVo.getCreator());
            divideclassRuleDao.updateByPrimaryKey(divideclassRule);
            if(StringUtils.isNotEmpty(divideclassRuleVo.getClassArr())){
                divideclassRuleVo.setId(divideclassRule.getId());
                saveClassArr(divideclassRuleVo,sysUser);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }


    public void saveClassArr(DivideclassRuleVo divideclassRuleVo,SysUser sysUser) {
        //判断家庭信息是否存在,存在就删除
        DivideclassClassVo d=new DivideclassClassVo();
        d.setDivideId(divideclassRuleVo.getDivideId());
        List<DivideclassClass> divideclasses=divideclassClassDao.getDivideclassClassList(d);
        Divideclass divideclass=divideclassDao.selectByPrimaryKey(divideclassRuleVo.getDivideId());
        if(divideclasses!=null&&divideclasses.size()>0){
            divideclassClassService.delete(d,sysUser);
        }
        if (StringUtils.isNotEmpty(divideclassRuleVo.getClassArr())) {
            List<DivideclassClassVo> divideclassVoList = JSON.parseArray(divideclassRuleVo.getClassArr(), DivideclassClassVo.class);
            if (divideclassVoList.size() > 0) {
                List<DivideclassClass> divideclassList = new ArrayList<>();
                for (DivideclassClassVo en : divideclassVoList) {
                    DivideclassClass divideclassClass=new DivideclassClass();
                    divideclassClass.setId(BaseUtil.uId());
                    divideclassClass.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
                    divideclassClass.setCreateTime(new Date());
                    divideclassClass.setDivideId(divideclassRuleVo.getDivideId());
                    divideclassClass.setHeadTeacher(en.getHeadTeacher());
                    divideclassClass.setName(en.getName());
                    if(divideclass!=null&&StringUtils.isNotEmpty(divideclass.getDivideType())){
                        divideclassClass.setParentId(divideclass.getDivideType());
                    }
                    divideclassClass.setNum(en.getNum());
                    divideclassClass.setAverage("0");
                    divideclassClass.setBnum("0");
                    divideclassClass.setGnum("0");
                    divideclassClass.setRuleId(divideclassRuleVo.getId());
                    divideclassClass.setCreator(sysUser.getId());
                    divideclassList.add(divideclassClass);
                }
                divideclassClassService.insertList(divideclassList);
            }
        }
    }


    /**
     * 根据条件逻辑删除
     *
     * @param divideclassRuleVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:24:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(DivideclassRuleVo divideclassRuleVo, SysUser sysUser) {
        //要修改的字段
        DivideclassRule divideclassRule = new DivideclassRule();
        divideclassRule.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        //筛选条件
        Example example = new Example(DivideclassRule.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(divideclassRuleVo.getId())) {
            criteria.andIn("id", Arrays.asList(divideclassRuleVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        divideclassRuleDao.updateByExampleSelective(divideclassRule, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param divideclassRuleVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:24:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(DivideclassRuleVo divideclassRuleVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(DivideclassRule.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(divideclassRuleVo.getId())) {
            criteria.andIn("id", Arrays.asList(divideclassRuleVo.getId().split(",")));
        }
        divideclassRuleDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}